package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.PermissionDao;
import com.itheima.dao.RoleDao;
import com.itheima.dao.UserDao;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 用户服务
 */
@Service(interfaceClass = UserService.class)
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private PermissionDao permissionDao;
   /*
    @Autowired
    private MenuDao menuDao;*/

    //根据用户名查询用户信息,包括用户的角色和角色关联的权限
    public User findByUsername(String username) {
        User user = userDao.findByUsername(username);//根据用户名查询用户表
        if (user == null) {
            return null;
        }
        Integer userId = user.getId();
        //根据用户id查询关联的角色
        Set<Role> roles = roleDao.findByUserId(userId);
        if (roles != null && roles.size() > 0) {
            //遍历角色集合，查询每个角色关联的权限
            for (Role role : roles) {
                Integer roleId = role.getId();//角色id
                //根据角色id查询关联的权限
                Set<Permission> permissions = permissionDao.findByRoleId(roleId);
                if (permissions != null && permissions.size() > 0) {
                    //角色关联权限集合
                    role.setPermissions(permissions);
                }
            }
            //用户关联角色集合
            user.setRoles(roles);
        }
        return user;
    }

    /*@Override
    public List<Menu> showMenuByUsername(String username) {

        //根据角色id查询菜单
        List<Menu> list = menuDao.findByRoleIds(username);
        Map<Integer,Menu> dataMap = list.stream().collect(Collectors.toMap(e->e.getId(), e->e));

        for (int i = 0; i < list.size(); i++) {
            Menu menu = list.get(i);
            if(menu.getParentMenuId() != null){
                Menu parent = dataMap.get(menu.getParentMenuId());
                if(null == parent){
                    parent = menuDao.findById(menu.getParentMenuId());
                    list.add(parent);
                    dataMap.put(parent.getId(),parent);
                }
                parent.getChildren().add(menu);
            }
        }

        List<Menu> rootMenus = list.stream().filter(e->null == e.getParentMenuId()).collect(Collectors.toList());
        return rootMenus;

    }*/

    @Override
    public List<Menu> getUserAndMenuList(String userName) {
        List<Menu> userAndMenuList = userDao.getUserAndMenuList(userName);


        return userAndMenuList;
    }

}
